//@author A0097091Y

package crossoff.logic;

import java.io.IOException;

import crossoff.db.History;
import crossoff.db.HistoryType;
import crossoff.db.Storage;
import crossoff.model.GUIobject;
import crossoff.model.Task;
import crossoff.ui.CrossOffGUI;

public class CommandDelTask {

	private static final String MESSAGE_DELETED = "TASK DELETED";

	public static GUIobject deleteTask(String command) throws IOException {
		
		Task toDelete;
		String[] commandSplit = command.split("\\s+");

		assert (Integer.parseInt(commandSplit[1]) >= 1) : "invalid input";
		assert (Integer.parseInt(commandSplit[1]) <= Storage.getInstance().taskCache.size()) : "invalid input";

		int taskIDToDelete = CrossOffGUI.getTaskID(Integer
				.parseInt(commandSplit[1]));

		String logMessage = MESSAGE_DELETED;
		
		toDelete = Storage.getInstance().getTaskFromID(taskIDToDelete);
		try {
			if (Storage.getInstance().deleteTask(taskIDToDelete)) {

				History.getInstance().addToUndo(toDelete, HistoryType.DELETED);
				return new GUIobject(logMessage, "");
			}
		} catch (IllegalArgumentException e) {
			System.err.println(e.getMessage());
		}
		
		return null;

	}
}
